package com.gzbugu.polytechsasia.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzbugu.polytechsasia.factory.ServiceFactory;
import com.gzbugu.polytechsasia.vo.Article;

public class UpdateArticle extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3951374006741190119L;

	/**
	 * 交由doPost方法执行
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request,response) ;
	}

	/**
	 * 修改文章
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id,type ;
		String content,title;
		Article article = new Article() ;
		response.setContentType("text/html;charset=UTF-8");
		try{
			id = Integer.parseInt(request.getParameter("id")) ;
			type = Integer.parseInt(request.getParameter("type")) ;
			title = (String)request.getParameter("title") ;
			content = (String)request.getParameter("content") ;
			
			article.setId(id) ;
			article.setType(type) ;
			if(title!=null){
				article.setTitle(title) ;
			}
			if(content!=null){
				article.setContent(content) ;
			}
			ServiceFactory.getUpdateArticleService().updateArticle(article) ;
			System.out.println(article.getContent());
			response.sendRedirect("en/admin/manage.jsp") ;
		} catch(Exception e){
			e.printStackTrace() ;
		}
	}

}
